using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using BowMaster.Resources;
using Services.Menu;

namespace BowMaster.Menu
{
    class MainMenu : DrawableGameComponent
    {

        GraphicsDeviceManager GraphicsDeviceManager;
        InputManager input_manager;
        Texture2D buttonbg, screen_bg;
        SpriteFont font, font_t, font_p;
        MyButton resume, levelSelect, credits, exit, reset, newgame, title, options;
        MyButton popup, popuptext, popupyes, popupno;
        SpriteBatch spriteBatch;
        StateManager StateManager;
        protected float press_wait;
        protected readonly float PRESS_INTERVAL = 0.5f;
        bool showpopup, showResume;
        public MainMenu(Game game, GraphicsDeviceManager graphics)
            : base(game)
        {
            this.GraphicsDeviceManager = graphics;
        }

        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            base.Initialize();
        }
        //int second_x = 210;
        //int first_x = 20;
        //int min_y = 40;
        //int large_width = 370;
        //int small_width = 180;
        //int distance = 10;
        //int height = 180;
        protected override void LoadContent()
        {
            //buttonbg = Game.Content.Load<Texture2D>("Levels/whiteTexture");
            buttonbg = Game.Content.Load<Texture2D>("Textures/emptyPixel");
            font = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");
            font_p = Game.Content.Load<SpriteFont>("Fonts/Arial26Ptx");
            font_t = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");
            screen_bg = Game.Content.Load<Texture2D>("Textures/LevelBackground");
            var whiteBox = Game.Content.Load<Texture2D>("Textures/bar_box");
            Color textColor = new Color (8,141,217);
            Color bgColor = Color.Green;
            showpopup = false;
            popup = new MyButton(new Rectangle(100, 300, 280, 200), Game.Content.Load<Texture2D>("Textures/popup"), Color.White);
            popuptext = new MyButton(new Rectangle(popup.rectangle.X + 10, popup.rectangle.Y + 10, popup.rectangle.Width - 20, popup.rectangle.Height / 2),
                Strings.Menu_AreSure, textColor, font_p, TextPos.Center, buttonbg, Color.White);
            popupno = new MyButton(new Rectangle(popup.rectangle.X + popup.rectangle.Width / 8, popup.rectangle.Y + popup.rectangle.Height / 2, popup.rectangle.Width / 4, 50),
                Strings.No, textColor, font_p, TextPos.Center, whiteBox, textColor);
            popupyes = new MyButton(new Rectangle(popup.rectangle.X + popup.rectangle.Width *5 / 8, popup.rectangle.Y + popup.rectangle.Height / 2, popup.rectangle.Width / 4, 50),
                Strings.Yes, textColor, font_p, TextPos.Center, whiteBox, textColor);
            //var a = StateManager.GameState.Current_level != 0 && StateManager.GameState.Current_level>=StateManager.levels.Count? Strings.Menu_Resume : Strings.Menu_NewGame;
            //resume = new MyButton(new Rectangle(first_x, min_y, large_width, height), Strings.Menu_Resume, textColor, font, bgColor, buttonbg);
            //newgame = new MyButton(new Rectangle(first_x, min_y + height + distance, small_width, height), Strings.Menu_NewGame, textColor, font, bgColor, buttonbg);
            //levelSelect = new MyButton(new Rectangle(second_x, min_y + height + distance, small_width, height), Strings.Menu_SelectLevel, textColor, font, bgColor, buttonbg);
            //credits = new MyButton(new Rectangle(first_x, min_y + (height + distance) * 2, large_width, height), Strings.Menu_Credits, textColor, font, bgColor, buttonbg);
            //exit = new MyButton(new Rectangle(second_x, min_y + (height + distance) * 3, small_width, height), Strings.Menu_Exit, textColor, font, bgColor, buttonbg);
            //reset = new MyButton(new Rectangle(first_x, min_y + (height + distance) * 3, small_width, height), Strings.Menu_ResetGame, Color.Red, font, bgColor, buttonbg);

            resume = new MyButton(new Rectangle(0, 220, 480, 80), Strings.Menu_Resume, textColor, font);
            newgame = new MyButton(new Rectangle(0, 300, 480, 80), Game.IsTrial()?Strings.TrialVersion:Strings.Menu_NewGame, textColor, font);
            levelSelect = new MyButton(new Rectangle(0, 380, 480, 80), Strings.Menu_SelectLevel, textColor, font);
            reset = new MyButton(new Rectangle(0, 470, 480, 60), Strings.Menu_ResetGame, textColor, font);
            credits = new MyButton(new Rectangle(0, 540, 480, 80), Strings.Menu_Credits, textColor, font);
            options = new MyButton(new Rectangle(0, 620, 480, 80), Strings.Menu_OptionsMenu, textColor, font);
            exit = new MyButton(new Rectangle(0, 700, 480, 80), Strings.Menu_Exit, textColor, font);

            //reset = new MyButton(new Rectangle(0, 380, 480, 80), Strings.Menu_ResetGame, textColor, font, bgColor, buttonbg);
            //credits = new MyButton(new Rectangle(0, 460, 480, 80), Strings.Menu_Credits, textColor, font, bgColor, buttonbg);
            //exit = new MyButton(new Rectangle(0, 540, 480, 80), Strings.Menu_Exit, textColor, font, bgColor, buttonbg);

            title = new MyButton(new Rectangle(20, 0, 440, 200), Strings.Menu_Title, textColor, font_t);

            showResume = StateManager.GameState.Available[0];

            if (Game.IsTrial())
                newgame.text = Strings.TrialVersion;
            //reset.textScale = 0.6f;
            base.LoadContent();
        }
        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));
            base.Dispose(disposing);
        }

        //Func<float, float> sin = x => (float)Math.Sin(x);
        //Func<float, float> cos = x => (float)Math.Cos(x);


        //int button_velocity = 2;
        //int resume_direction = 1;
        //int newgame_direction = 1;
        //int select_direction = 1;
        //int credits_direction = 1;
        //float text_rotation_velocity = 0.1f;
        public override void Update(GameTime gameTime)
        {
            if (press_wait < PRESS_INTERVAL)
                press_wait += (float)gameTime.ElapsedGameTime.TotalSeconds;

            //var t = (float)gameTime.TotalGameTime.TotalSeconds;
            
            //newgame.textRotation = sin(t * 3) * text_rotation_velocity;
            //resume.textRotation = sin(t * 3) * text_rotation_velocity;
            //levelSelect.textRotation = sin(t * 3) * text_rotation_velocity;
            //credits.textRotation = sin(t * 3) * text_rotation_velocity;
            //exit.textRotation = sin(t * 3) * text_rotation_velocity;
            //reset.textRotation = sin(t * 3) * text_rotation_velocity;
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                if (showpopup)
                    showpopup = false;
                else
                    Game.Exit();
                //return;
            } 
                base.Update(gameTime);
        }

        Rectangle bg_rect = new Rectangle(0, 0, 480, 800);
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            
            spriteBatch.Begin();

            spriteBatch.Draw(screen_bg, bg_rect, Color.White);

            title.Draw(spriteBatch);
            if(showResume)
                resume.Draw(spriteBatch);
            newgame.Draw(spriteBatch);
            levelSelect.Draw(spriteBatch);
            credits.Draw(spriteBatch);
            exit.Draw(spriteBatch);
            reset.Draw(spriteBatch);
            options.Draw(spriteBatch);
            //sin(t * 3.0f) * velocity,
            if (showpopup)
            {
                popup.Draw(spriteBatch);
                popuptext.Draw(spriteBatch);
                popupno.Draw(spriteBatch);
                popupyes.Draw(spriteBatch);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
        Point tp = new Point();
        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;
            tp.X = (int)tap_position.X;
            tp.Y = (int)tap_position.Y;
            if (showpopup)
            {
                if (popupyes.tapped_on(tp))
                {
                    StateManager.ResetGameState();
                    showResume = false;
                    showpopup = false;
                }
                else if (popupno.tapped_on(tp))
                {
                    showpopup = false;
                }
                else
                    if (!popup.tapped_on(tp))
                        showpopup = false;
            }
            else
            {
                if (exit.tapped_on(tp))
                    Game.Exit();
                else if (newgame.tapped_on(tp))
                {
                    StateManager.GameState.Current_level = 0;
                    Storage.SaveGameState(StateManager.GameState);
                    StateManager.Resume();
                }
                else if (resume.tapped_on(tp) && showResume)
                {
                    StateManager.Resume();
                }
                else if (reset.tapped_on(tp))
                {
                    showpopup = true;
                    //resume.text = Strings.Menu_NewGame;
                }
                else if (levelSelect.tapped_on(tp))
                    StateManager.SelectLevelSelectMenu();
                else if (options.tapped_on(tp))
                    StateManager.SelectOptionsMenu();
                else if (credits.tapped_on(tp))
                    StateManager.SelectCreditsMenu();
                else if (title.tapped_on(tp))
                    StateManager.SelectCreditsMenu();
            }
        }
    }
}
